define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'projectproduct/index' + location.search,
                    add_url: 'projectproduct/add',
                    edit_url: 'projectproduct/edit',
                    del_url: 'projectproduct/del',
                    multi_url: 'projectproduct/multi',
                    table: 'project_product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'project.name', title: __('Project.name')},
                        {field: 'product.name', title: __('Product.name')},
                        {field: 'category_number', title: __('Category_number')},
                        {field: 'product.unit', title: "计量单位"},
                        {field: 'product.decomposition', title: "分解单位"},
                        {field: 'is_volume', title: __('Is_volume'), searchList: {"1":__('Is_volume 1'),"2":__('Is_volume 2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
                $(document).on("change", "#c-product_id", function () {
                    $.post("product/unit",{
                    //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                        custom: {id: $('#c-product_id').val()},
                        keyField:'id',
                        showField:'unit_name',
                        },function(result){
                            console.log(result);
                            //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                            $('#c-unit_id').val(result.unit+'->'+result.decomposition);
                        });
                })
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("change", "#c-product_id", function () {
                    $.post("product/unit",{
                    //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                        custom: {id: $('#c-product_id').val()},
                        keyField:'id',
                        showField:'unit_name',
                        },function(result){
                            console.log(result);
                            //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                            $('#c-unit_id').val(result.unit+'->'+result.decomposition);
                        });
                })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});